<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}1111111</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		async onLoad() {
			let link = window.location.href;
			let code = null;
			let id = null;
			let actid = null;
			let temp = link.split("state=")[1];
			actid = temp.split("&")[0];
			//获取当前页面的url
			// //判断link中有没有code=字符串  
			if(link.indexOf('code=') == -1){
				//没有code则引导关注者打开如下页面链接，授权
				let uri = 'https://app.buildream.club/signup'
				//let uri ='http://47.108.115.71:9361/signup'
				uri = encodeURIComponent( uri );
				let appid = "wx070fe96753c674a0";
				let authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_base&state=${actid}#wechat_redirect`;
				//重新引导回本页面，然后又重新判断link.indexOf('code=') == -1
				window.location.href = authURL;
			}else{
				 //回调函数已经执行 返回的链接存在code= 地址解析
				let temp = link.split("code=")[1];
				alert(temp)
				code = temp.split("&")[0];
				let tempid = temp.split("state=")[1];
				id = tempid.split('#')[0]
				let data = { code:code, id:id }
				//对getUserOpenid云函数发送请求传参code获取用户openid
				let res = await this.$api.request("v2/guest/oauth2accessToken",data);
				uni.navigateTo({url:'./signActList?id='+res.data.openid})	
				
				
			}
		},
		

		methods: {
			init(){
				uni.login({
				  provider: 'weixin',
				  success: function (loginRes) {
				   //loginRes中有code，拿着code再请求自己服务器以获取openid
				    console.log(loginRes.authResult);
				  }
				});
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
